package com.ruoyi.partybuilding.common;

import com.easy.query.core.api.dynamic.sort.ObjectSort;
import com.easy.query.core.api.dynamic.sort.ObjectSortBuilder;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.ruoyi.common.core.page.PageDomain;
import io.swagger.annotations.ApiModelProperty;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;


public class PageSort extends PageDomain implements ObjectSort {

    @Override
    public void configure(ObjectSortBuilder objectSortBuilder) {
        if (StringUtils.isNotBlank(getOrderByName()))
            objectSortBuilder.orderBy(this.getOrderByName(), !isDescIs());
    }
    public static PageSort toMaxSize(){
        var page=new PageSort();
        page.setPageSize(Integer.MAX_VALUE);
        page.setPageNum(1);
        return  page;
    }
    public static <T> Page<T> toPageData(EasyPageResult<T> pData) {
        return new PageImpl<>(pData.getData(), Pageable.unpaged(), pData.getTotal());
    }
    /**
     * 当前记录起始索引
     */
    private Integer pageNum = 1;

    /**
     * 每页显示记录数
     */
    private Integer pageSize = 10;

    /**
     * 排序列
     */
    @ApiModelProperty(value = "",hidden = true)
    private String orderByColumn;

    public boolean isDescIs() {
        return descIs;
    }

    public void setDescIs(boolean descIs) {
        this.descIs = descIs;
    }

    @ApiModelProperty(value = "是否降序，默认false升序")
    boolean descIs;


    public String getOrderByName() {
        return orderByName;
    }

    public void setOrderByName(String orderByName) {
        if (StringUtils.isNotBlank(orderByName) && StringUtils.isNotBlank(orderByColumn))
            throw new IllegalArgumentException("orderByColumn与orderByName不可同时设置！");
        this.orderByName = orderByName;
    }
    public boolean orderByIs(){
        return StringUtils.isBlank(getOrderByName());
    }
    @ApiModelProperty(value = "排序属性名称")
    String orderByName;

    /**
     * 兼容rouyi,请勿使用
     *
     * @return
     */
    @ApiModelProperty(value = "",hidden = true)
    @Deprecated
    @Override
    public String getOrderBy() {
        return super.getOrderBy();
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        if (pageNum == null || pageNum < 1)
            this.pageNum = 1;
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null || pageSize < 1)
            this.pageSize = 10;
        this.pageSize = pageSize;
    }

    /**
     * 兼容rouyi,请勿使用
     *
     * @return
     */
    @ApiModelProperty(value = "",hidden = true)
    @Deprecated
    @Override
    public String getOrderByColumn() {
        return orderByColumn;
    }

    /**
     * 兼容rouyi,请勿使用
     *
     * @return
     */
    @ApiModelProperty(value = "",hidden = true)
    @Deprecated
    @Override
    public void setOrderByColumn(String orderByColumn) {
        if(StringUtils.isNotBlank(orderByName)&&StringUtils.isNotBlank(orderByColumn))
            throw new IllegalArgumentException("orderByColumn与orderByName不可同时设置！");
        this.orderByColumn = orderByColumn;
    }

    /**
     * 兼容rouyi,请勿使用
     *
     * @return
     */
    @ApiModelProperty(value = "",hidden = true)
    @Deprecated
    @Override
    public String getIsAsc() {
        return descIs ? "desc" : "asc";
    }

    /**
     * 兼容rouyi,请勿使用
     *
     * @return
     */
    @ApiModelProperty(value = "",hidden = true)
    @Deprecated
    @Override
    public void setIsAsc(String isAsc) {
        if (StringUtils.isNotBlank(isAsc))
            return;
        if ("desc".equals(isAsc) || "DESC".equals(isAsc) || "descending".equals(isAsc))
            setDescIs(true);

    }

}
